package com.fengjing.launcher.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Outline;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.Toast;

import com.fengjing.launcher.R;
import com.hg.library.config.FParser;

import java.io.File;

public class MainActivity extends BaseActivity implements View.OnFocusChangeListener, View.OnClickListener {
    private MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, new IntentFilter("com.ariplane.checkservice.overtime"));
        startActivity(new Intent(this, EyeActivity.class));

        findViewById(R.id.btn_01).setOnFocusChangeListener(this);
        findViewById(R.id.btn_01).setOnClickListener(this);
        findViewById(R.id.btn_01).setClipToOutline(true);
        findViewById(R.id.btn_01).setOutlineProvider(viewOutlineProvider);
        findViewById(R.id.btn_02).setOnFocusChangeListener(this);
        findViewById(R.id.btn_02).setOnClickListener(this);
        findViewById(R.id.btn_02).setClipToOutline(true);
        findViewById(R.id.btn_02).setOutlineProvider(viewOutlineProvider);
        findViewById(R.id.btn_03).setOnFocusChangeListener(this);
        findViewById(R.id.btn_03).setOnClickListener(this);
        findViewById(R.id.btn_03).setClipToOutline(true);
        findViewById(R.id.btn_03).setOutlineProvider(viewOutlineProvider);
        findViewById(R.id.btn_04).setOnFocusChangeListener(this);
        findViewById(R.id.btn_04).setOnClickListener(this);
        findViewById(R.id.btn_04).setClipToOutline(true);
        findViewById(R.id.btn_04).setOutlineProvider(viewOutlineProvider);
    }

    private ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
        @Override
        public void getOutline(View view, Outline outline) {
            outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 15);
        }
    };

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            v.animate().scaleX(1.1f).scaleY(1.1f).start();
            v.setElevation(10);
        } else {
            v.animate().scaleX(1).scaleY(1).start();
            v.setElevation(0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_01:
                startActivity(new Intent(this, ListActivity.class).putExtra("rootPath", FParser.BASE_PATH + "videos/VRty"));
                break;
            case R.id.btn_02:
                startActivity(new Intent(this, CategoryActivity.class));
                break;
            case R.id.btn_03:
                startActivity(new Intent(this, ListActivity.class).putExtra("rootPath", FParser.BASE_PATH + "videos/smcj"));
                break;
            case R.id.btn_04:
                Toast.makeText(this, "正在开发中...", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            SharedPreferences.Editor editor = getSharedPreferences("record", MODE_PRIVATE).edit();
            editor.putBoolean("hasLocked", true);
            editor.commit();
            try {
                File file = new File(Environment.getExternalStorageDirectory()+"/"+"lock.txt");
                if(!file.exists()){
                    file.createNewFile();
                }
            }catch (Exception e){

            }
            startActivity(new Intent(MainActivity.this, LockActivity.class));
        }
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(myReceiver);
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
